// JavaScript Document


(function(){
	
	//加载天数
	function addDay(num, date){
		var now = date ? new Date(date.getFullYear(), date.getMonth(), date.getDate()) : new Date();
		var olddate = now.getDate();
		now.setDate(now.getDate()+num);
		return now.getFullYear() + '-' + ((now.getMonth()+1)<10 ? "0"+(now.getMonth()+1): (now.getMonth()+1))+'-'+ (now.getDate()<10 ? "0"+now.getDate() :now.getDate());
	}	
		

	
	
	// 检索变量
	
    var id_startDate = MGTOOL.$id('id_startDate'),
        id_backDate = MGTOOL.$id('id_backDate');
		if(id_startDate && id_backDate){
			if(id_startDate.value == '') {
				id_startDate.value = addDay(1);
			}
			if (id_backDate.value == '') {
				id_backDate.value = addDay(2);
			}
			var flightstart = new Datepicker(id_startDate, {eachW:206,ttMonth:2, addDays:360, callback:callhoteldatestart}),
			
			   firstDate = id_startDate.value.split('-'), //获取起始日期数据
			   today = (!!firstDate.length) ? new Date(firstDate[0], firstDate[1]-1, firstDate[2]) : new Date(),//去开始日期时间作为结束日期的起始时间,否则以今天开始
			   f_s_Date = addDay(1, today).split('-'),
			   diffDays = Math.ceil((today-new Date())/86400000), //获取开始日期距离今天的时间差
			   f_e_Date = addDay(360, new Date()).split('-'); //默认开始日期的endDate
			   
		    var flightend = new Datepicker(id_backDate, {eachW:206,ttMonth:2, startDate:[f_s_Date[0], (f_s_Date[1]-1), f_s_Date[2]], addDays:parseInt(120+diffDays), endDate:f_e_Date, callback:callhoteldateend});
			
		}

		
	// 定义日历控件回调函数
	function callhoteldatestart(o, t){
	    o.value = t.title;
		o.style.color = '#333';
		var firstDate = o.value.split('-');
		var sdd = new Date(firstDate[0], firstDate[1]-1, firstDate[2]);
		var f_s_Date = addDay(1, sdd).split('-');
		id_backDate.value = addDay(1, sdd); //结束日期始终比开始日期晚一天
		var diffDays = Math.ceil((sdd-new Date())/86400000); //获取开始日期距离今天的时间差
		var f_e_Date = addDay(120, new Date()).split('-');
		flightend = new Datepicker(id_backDate, {eachW:206,ttMonth:2, startDate:[f_s_Date[0], (f_s_Date[1]-1), f_s_Date[2]], addDays:parseInt(120+diffDays), endDate:f_e_Date, callback:callhoteldateend});
		if(MGTOOL.$id('godate_content2').style.display === 'block'){
		    window.setTimeout(function(){id_backDate.focus();}, 0);
		}
		
	}
	
	function callhoteldateend(o, t){
	    o.value = t.title;
		o.style.color = '#333';
	}			
		
	
})();



